package com.hsteach.task;

import com.hsteach.common.utils.StringUtils;
import com.hsteach.dao.domain.buildingApply.BuildingReviewPerson;
import com.hsteach.dao.domain.equipment.EquipmentRepairConfig;
import com.hsteach.dao.domain.patrol.Teacher;
import com.hsteach.dao.mapper.PatrolRoleMapper;
import com.hsteach.dao.mapper.buildingApply.BuildingReviewPersonMapper;
import com.hsteach.dao.mapper.equipment.EquipmentRepairConfigMapper;
import com.hsteach.enums.BooleanEnum;
import com.hsteach.framework.feign.client.xgk.dto.ret.GetPersonByConditionRet;
import com.hsteach.framework.feign.client.xgk.dto.ret.PersonRet;
import com.hsteach.framework.web.service.AuthService;
import com.hsteach.framework.web.service.UApiService;
import com.hsteach.service.third.XgkService;
import com.hsteach.web.dto.ScoreRolePageDto;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 归档上月值班表
 *
 */
@Slf4j
@Component
public class SyncUserTask {

    @Autowired
    private EquipmentRepairConfigMapper equipmentRepairConfigMapper;

    @Autowired
    private BuildingReviewPersonMapper buildingReviewPersonMapper;

    @Resource
    public PatrolRoleMapper patrolRoleMapper;

    @Autowired
    private XgkService xgkService;

    @Autowired
    private AuthService authService;

    @Autowired
    private UApiService uApiService;

    @Scheduled(cron = "0 0 0 * * ?")
    public void syncUser() {
        log.info("开始同步维修用户信息");
        List<EquipmentRepairConfig> repairConfigs = equipmentRepairConfigMapper.queryList();
        if (CollectionUtils.isEmpty(repairConfigs)){
            return;
        }
        List<String> personIds = repairConfigs.stream().map(EquipmentRepairConfig::getPersonId).distinct().collect(Collectors.toList());
        Map<String, PersonRet> personMap = xgkService.getPersonInfoList(personIds).stream().collect(Collectors.toMap(PersonRet::getPersonId, person -> person));
//        log.info("维修用户信息同步--根据ids：{}查询到用户信息：{}",personIds,personMap);
        //获取幼教空间信息
        String appToken = authService.getAppTokenByIdKey("VIsdrbXuqF", "67f1b7e7f0ee4b44bfc0e857da1f8a6a");
        String accessToken = authService.getAccessTokenByNP();
        Map<String, GetPersonByConditionRet> conditionRetMap = uApiService.getMaintenancePerson(appToken, accessToken).stream().collect(Collectors.toMap(GetPersonByConditionRet::getUserId, person -> person));
        List<EquipmentRepairConfig> newConfigs = new ArrayList<>();
        repairConfigs.forEach(e ->{
            PersonRet person = personMap.get(e.getPersonId());
            if (ObjectUtils.isEmpty(person) || ObjectUtils.isEmpty(conditionRetMap.get(e.getTPersonId()))){
                e.setPersonName(null);
                e.setPersonId(null);
                newConfigs.add(e);
            } else if (!person.getName().equals(e.getPersonName())
                    || !person.getOrgCode().equals(e.getOrgCode())
                    || !person.getMobile().equals(e.getPersonMobile())){
                e.setPersonName(person.getName());
                e.setPersonMobile(person.getMobile());
                newConfigs.add(e);
            }
//            log.info("维修用户信息同步--过滤后的数据信息：{}",e);
        });
        if (!CollectionUtils.isEmpty(newConfigs)){
            equipmentRepairConfigMapper.batchUpdate(newConfigs);
        }
        log.info("同步用户信息完成");
    }


    @Scheduled(cron = "0 5 0 * * ?")
    public void syncVenueUser() {
        log.info("开始场馆审核用户信息");
        List<BuildingReviewPerson> buildingReviewPeople = buildingReviewPersonMapper.queryList();
        if (CollectionUtils.isEmpty(buildingReviewPeople)){
            return;
        }
        List<String> personIds = buildingReviewPeople.stream().map(BuildingReviewPerson::getReviewPersonId).filter(StringUtils::isNotEmpty).collect(Collectors.toList());
        Map<String, PersonRet> personMap = xgkService.getPersonInfoList(personIds).stream().collect(Collectors.toMap(PersonRet::getPersonId, person -> person));
        List<BuildingReviewPerson> newReview = new ArrayList<>();
        buildingReviewPeople.forEach(e ->{
            PersonRet person = personMap.get(e.getReviewPersonId());
            if (ObjectUtils.isEmpty(person)){
                e.setReviewPersonName(null);
                e.setReviewPersonId(null);
                newReview.add(e);
            } else if (!person.getName().equals(e.getReviewPersonName())
                    || !person.getOrgCode().equals(e.getOrgCode())){
                e.setReviewPersonName(person.getName());
                newReview.add(e);
            }
        });
        if (!CollectionUtils.isEmpty(newReview)) {
            buildingReviewPersonMapper.batchUpdate(newReview);
        }
        log.info("同步场馆审核用户信息完成");
    }

    @Scheduled(cron = "0 10 0 * * ?")
    public void syncInspectUser() {
        log.info("开始班级评比用户信息");
        List<Teacher> teachers = patrolRoleMapper.queryTeacherList(new ScoreRolePageDto());
        if (CollectionUtils.isEmpty(teachers)){
            return;
        }
        List<String> personIds = teachers.stream().map(Teacher::getTeacherId).collect(Collectors.toList());
        Map<String, PersonRet> personMap = xgkService.getPersonInfoList(personIds).stream().collect(Collectors.toMap(PersonRet::getPersonId, person -> person));
        List<Teacher> newTeacher = new ArrayList<>();
        teachers.forEach(e ->{
            PersonRet person = personMap.get(e.getTeacherId());
            if (ObjectUtils.isEmpty(person)){
                e.setIsDelete(BooleanEnum.TRUE.getCode());
                newTeacher.add(e);
            } else if (!person.getName().equals(e.getTeacherName())
                    || !person.getOrgCode().equals(e.getOrgCode())
                    || !person.getMobile().equals(e.getTeacherNumber())){
                e.setTeacherName(person.getName());
                e.setTeacherNumber(person.getMobile());
                newTeacher.add(e);
            }
        });
        if (!CollectionUtils.isEmpty(newTeacher)) {
            patrolRoleMapper.batchUpdate(newTeacher);
        }
        log.info("同步班级评比用户信息完成");
    }
}
